//
//  BaskWallLayer.cpp
//  firebkball
//
//  Created by flydy on 2018/5/28.
//
//

#include "BaskWallLayer.h"
#include "LogicData.h"
#include "VisibleRect.h"
USING_NS_CC;

bool BaskWallLayer::init() {
    if ( !Layer::init() )
    {
        return false;
    }
    setIgnoreAnchorPointForPosition(false);
    setAnchorPoint(Vec2::ANCHOR_MIDDLE);
    return true;
}

void BaskWallLayer::onEnter() {
    Layer::onEnter();
    
    Size winsize = Director::getInstance()->getWinSize();
    auto visibleSize = Director::getInstance()->getVisibleSize();
    Vec2 origin = Director::getInstance()->getVisibleOrigin();
    Vec2 s_centre = Vec2(visibleSize.width / 2 + origin.x, visibleSize.height / 2 + origin.y);
    
    LogicData *lgd = LogicData::getInstance();
    
    float kuangRadius = 4.0f;
    
    //两边墙的宽度
    int wallW = 10;
    
    this->size = visibleSize;
    this->position = s_centre;
    
    setIgnoreAnchorPointForPosition(false);
    setAnchorPoint(Vec2::ANCHOR_MIDDLE);
    setContentSize(size);
    setPosition(s_centre);
    
    mainNode = CSLoader::createNode("res/lankuan.csb");
    mainNode->setPosition(lgd->lankuangPosition);
    this->addChild(mainNode);
    
    auto nd_left = mainNode->getChildByName("nd_left");
    auto nd_right = mainNode->getChildByName("nd_right");
    auto nd_bottom_left = mainNode->getChildByName("nd_bottom_left");
    auto nd_bottom_right = mainNode->getChildByName("nd_bottom_right");
    
    //墙的位置
    Size wallSize = Size(wallW, visibleSize.height);
    Node *leftWall = Node::create();
    leftWall->setContentSize(wallSize);
    leftWall->setIgnoreAnchorPointForPosition(false);
    leftWall->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
    leftWall->setPosition(Vec2(wallW/2, s_centre.y));
    auto boundBody = PhysicsBody::createEdgeBox( wallSize, PhysicsMaterial(1.0f, 2.0f, 1.0f) , 1);
    boundBody->setCategoryBitmask(lgd->wallMask);
    boundBody->setCollisionBitmask(lgd->wallMask | lgd->ball1Mask);
    boundBody->setContactTestBitmask(lgd->wallMask | lgd->ball1Mask);
    boundBody->setDynamic(false);
    leftWall->setTag(lgd->wallTag);
    boundBody->setTag(lgd->wallTag);
    leftWall->setPhysicsBody(boundBody);
    addChild(leftWall);

    Node *rightWall = Node::create();
    rightWall->setContentSize(wallSize);
    rightWall->setIgnoreAnchorPointForPosition(false);
    rightWall->setAnchorPoint(Vec2::ANCHOR_MIDDLE);
    rightWall->setPosition(Vec2(size.width-wallW/2, s_centre.y));
    boundBody = PhysicsBody::createEdgeBox( wallSize, PhysicsMaterial(1.0f, 2.0f, 1.0f) , 1);
    boundBody->setCategoryBitmask(lgd->wallMask);
    boundBody->setCollisionBitmask(lgd->wallMask | lgd->ball1Mask);
    boundBody->setContactTestBitmask(lgd->wallMask | lgd->ball1Mask);
    boundBody->setDynamic(false);
    rightWall->setTag(lgd->wallTag);
    boundBody->setTag(lgd->wallTag);
    rightWall->setPhysicsBody(boundBody);
    addChild(rightWall);

    
    //篮筐左边
    boundBody = PhysicsBody::createCircle(kuangRadius, PhysicsMaterial(1.0f, 2.0f, 1.0f));
    boundBody->setCategoryBitmask(lgd->kuangMask);
    boundBody->setCollisionBitmask(lgd->kuangMask | lgd->ball1Mask);
    boundBody->setContactTestBitmask(lgd->kuangMask | lgd->ball1Mask);
    boundBody->setDynamic(false);
    nd_left->setPhysicsBody(boundBody);
    nd_left->setTag(lgd->kuangTag);
    boundBody->setTag(lgd->kuangTag);
    
    //篮筐右边
    boundBody = PhysicsBody::createCircle(kuangRadius, PhysicsMaterial(1.0f, 2.0f, 1.0f));
    boundBody->setCategoryBitmask(lgd->kuangMask);
    boundBody->setCollisionBitmask(lgd->kuangMask | lgd->ball1Mask);
    boundBody->setContactTestBitmask(lgd->kuangMask | lgd->ball1Mask);
    boundBody->setDynamic(false);
    nd_right->setPhysicsBody(boundBody);
    nd_right->setTag(lgd->kuangTag);
    boundBody->setTag(lgd->kuangTag);
    
    //上下碰撞进球判断物件,被碰到后消失
    boundBody = PhysicsBody::createCircle(kuangRadius, PhysicsMaterial(1.0f, 0, 1.0f));
    boundBody->setCategoryBitmask(lgd->ropeMask);
    boundBody->setCollisionBitmask(lgd->ropeMask | lgd->ball1Mask);
    boundBody->setContactTestBitmask(lgd->ropeMask | lgd->ball1Mask);
    boundBody->setDynamic(false);
    nd_bottom_left->setPhysicsBody(boundBody);
    nd_bottom_left->setTag(lgd->ropeTag);
    boundBody->setTag(lgd->ropeTag);
    
    boundBody = PhysicsBody::createCircle(kuangRadius, PhysicsMaterial(1.0f, 0, 1.0f));
    boundBody->setCategoryBitmask(lgd->ropeMask);
    boundBody->setCollisionBitmask(lgd->ropeMask | lgd->ball1Mask);
    boundBody->setContactTestBitmask(lgd->ropeMask | lgd->ball1Mask);
    boundBody->setDynamic(false);
    nd_bottom_right->setPhysicsBody(boundBody);
    nd_bottom_right->setTag(lgd->ropeTag);
    boundBody->setTag(lgd->ropeTag);

}
